package cn.qidisoft.edu.hzjt.service.zyc.impl;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.zyc.ScientificNoticeMapper;
import cn.qidisoft.edu.hzjt.model.zyc.SchoolMagazine;
import cn.qidisoft.edu.hzjt.model.zyc.ScientificNotice;
import cn.qidisoft.edu.hzjt.model.zyc.ScientificNoticeReader;
import cn.qidisoft.edu.hzjt.service.zyc.IScientificNoticeService;
import cn.qidisoft.edu.hzjt.utils.LogType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ScientificNoticeServiceImpl implements IScientificNoticeService{

    protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
            .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    ScientificNoticeMapper scientificNoticeMapper;

    @Override
    public PaginationBean<ScientificNotice> queryPageList(Map<String, Object> data, Page page) {
        try {
            int c = scientificNoticeMapper.queryPageListcount(data);
            int totalRecords = c;
            PaginationBean<ScientificNotice> pageHolder = new PaginationBean<>(page, totalRecords);
            List<ScientificNotice> resultList = scientificNoticeMapper
                    .queryPageList(data,
                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
        }
    }

    @Override
    public ScientificNotice queryByPK(ScientificNotice record) {
        return scientificNoticeMapper.queryByPK(record);
    }

    @Override
    public Integer insert(ScientificNotice record) {
        scientificNoticeMapper.insert(record);
        if (StringUtils.isNotEmpty(record.getGid())){
            ScientificNotice nr = queryByPK(record);
            if (nr!=null&&nr.getXxid()!=null) {
                int newid = nr.getXxid();
                record.setXxid(newid);
                return newid;
            }
        }
        return null;
    }

    @Override
    public int update(ScientificNotice record) {
        scientificNoticeMapper.update(record);
        return 0;
    }

    @Override
    public int delete(ScientificNotice record) {
        scientificNoticeMapper.delete(record);
        return 0;
    }

    @Override
    public int validation(ScientificNotice record) {
        return scientificNoticeMapper.validation(record);
    }

	@Override
	public ScientificNotice getPlanning(ScientificNotice sMagazine) {
		
		return scientificNoticeMapper.getPlanning(sMagazine);
	}

	@Override
	public List<ScientificNotice> getPlanningList(Map<String, Object> queryMap) {
		
		return scientificNoticeMapper.getPlanningList(queryMap);
	}

	@Override
	public void insertReader(List<ScientificNoticeReader> list) {
		scientificNoticeMapper.insertReader(list);
		
	}

	@Override
	public List<ScientificNoticeReader> getReader(int parseInt) {
		return scientificNoticeMapper.getReader(parseInt);
	}

	@Override
	public void deleteReader(Integer id) {
		scientificNoticeMapper.deleteReader(id);
		
	}
}
